package com.middlegroup.subjectpay.web.api;

import com.middlegroup.common.enums.ServiceType;
import com.middlegroup.common.model.PayRequest;
import com.middlegroup.common.model.ResponseEntity;
import com.middlegroup.common.model.TransResult;
import com.middlegroup.subjectpay.service.WechatPayH5Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 *
 * @author chengweiwei
 * @date 2019/8/6
 */
@RestController
public class WechatPayH5Controller extends BaseController {
    @Resource
    private WechatPayH5Service wechatPayH5Service;

    /**通用微信H5支付(MWEB)统一下单接口
     curl -i 'http://10.0.0.180:8089/wechatPayH5' -d 'state=客户端预留字段&notifyUrl=通知回调url&ip=116.226.45.158&amount=0.01&serviceType=WECHATPAY&subjectCode=S000001&channelCode=WECHATPAYH5&merchantBillNo=merchantBillNo201908070001&billNo=billNo201908070001'
     * @param transRequest 交易参数
     * @return 通用json返回
     */
    @RequestMapping(value = "wechatPayH5")
//    public ResponseEntity wechatPayH5(@Valid PayRequest transRequest, @SessionSubjectCode String subjectCode){
    public ResponseEntity wechatPayH5(@Valid PayRequest transRequest){
        ResponseEntity ret = new ResponseEntity(ResponseEntity.STATUS_OK);
        transRequest.setServiceType(ServiceType.wx_h5.name());
        //初始化交易结果
        TransResult transResult = new TransResult();
        transResult.setMerchantBillNo(transRequest.getMerchantBillNo());
        transResult.setBillNo(transRequest.getBillNo());
        transResult.setAmount(transRequest.getAmount());
        transResult.setTransStatus(0);
        transResult.setMsg("微信H5支付MWEB统一下单处理中");
        transResult.setTransTime(System.currentTimeMillis());
        ret.setData(transResult);

        //调用微信H5支付(MWEB)统一下单接口
        transResult = wechatPayH5Service.wechatPayH5(transRequest);

        if (null != transResult){
            return new ResponseEntity(ResponseEntity.STATUS_OK,transResult);
        }

        //如果通用微信H5支付(MWEB)统一下单接口接口异常，则默认代扣处理中，防止重复交易
        transResult.setTransStatus(0);
        transResult.setMsg("微信H5支付(MWEB)统一下单接口服务异常!请联系管理员核对状态!");
        transResult.setTransTime(System.currentTimeMillis());
        return ret;
    }
}
